const glProps = [
  'globe', 'geo3D', 'mapbox3D', 'grid3D', 'xAxis3D', 'yAxis3D', 'zAxis3D'
]

const glSeries = [
  'scatter3D', 'bar3D', 'line3D', 'lines3D', 'map3D', 'surface', 'polygons3D', 'scatterGL', 'graphGL', 'flowGL'
]

function isGlRequired(option) {
  return glProps.some(prop => !!option[prop]) || (option.series && option.series.some(item => glSeries.indexOf(item.type) !== -1))
}

export default {
  /**
   * 自动加载第三方包
   * @param option
   * @return {Promise<void>}
   */
  async load(option) {
    if (!option) {
      return
    }
    // echarts-gl
    if (isGlRequired(option)) {
      return import(/* webpackChunkName: "echarts-gl" */'echarts-gl')
    }

    if (!option.series) {
      return
    }

    await Promise.all(option.series.map(async ({type}) => {
      if (!type) {
        return
      }

      if (type === 'wordCloud') {
        return import(/* webpackChunkName: "echarts-wordcloud" */'echarts-wordcloud')
      }

      if (option.series.some(item => item.type === 'liquidFill')) {
        return import(/* webpackChunkName: "echarts-liquidfill" */'echarts-liquidfill')
      }
    }))
  }
}
